<?php
/**
 * common model file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/09/04
 * Time: 17:52
 */

namespace HiCommon\Model;

class OrderCheckpointVehicle extends Base
{

    protected $table = 'order_checkpoint_vehicle';

    protected $guarded = [];

    //public $timestamps = false;

    /**
     * 应该被调整为日期的属性
     *
     * @var array
     */
    protected $dates
      = [
      ];

    /**
     * 关联用户车辆表
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function user_vehicle(){
        //        ->select(['id','name','parentid'])
        return $this->belongsTo(UsersVehicle::class,'users_vehicle_id')->with('simple_vehicle_model');
    }

    /**
     * 关联代驾子表
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function helpdriving()
    {
        return $this->belongsTo(OrderCheckpointVehicleHelpdriving::class, 'order_sn', 'order_sn');
    }


    /**
     * 关联年检预约表
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function order_checkpoint()
    {
        return $this->belongsTo(OrderCheckpoint::class, 'order_checkpoint_id');
    }

    /**
     * 关联年检预约表
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function order_simple()
    {
        return $this->belongsTo(OrderCheckpoint::class, 'order_checkpoint_id')->select(['id','order_sn','main_type','user_id','shop_id']);
    }

    /**
     * 关联退款申请表
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function refund_success()
    {
        return $this->hasOne(OrderCheckpointRefund::class,  'order_checkpoint_vehicle_id')->where('apply_refund_state',3);
    }





    /**
     * 获取状态对应的名称
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getStateAttribute($value)
    {
        switch ($value) {
            case 0:
                $res = ['id' => 0, 'name' => '草稿'];
                break;
            case 1:
                $res = ['id' => 1, 'name' => '待审核'];
                break;
            case 2:
                $res = ['id' => 2, 'name' => '通过'];
                break;
            case 3:
                $res = ['id' => 3, 'name' => '拒绝'];
                break;
            default:
                $res = ['id' => -1, 'name' => '未知'];
        }
        return $res;
    }

    /**
     * 获取图片可用链接
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getImgUrlAttribute($value)
    {
        $arr_data = [
          'src'      => $value,
          'full_src' => oss_full_url($value),
        ];
        return $arr_data;
    }

    //申请退款状态：0无 1申请中 2申请成功-待退款 （通过）3申请成功-已退款 （通过）9申请失败（拒绝）
    public function name_apply_refund_state($state,&$arr_data){
        switch ($state)
        {
            case 1:
                $arr_data['apply_refund_state'] = '申请中';
                break;
            case 2:
                $arr_data['apply_refund_state'] = '申请成功-待退款';
                break;
            case 3:
                $arr_data['apply_refund_state'] = '申请成功-已退款';
                break;
            case 9:
                $arr_data['apply_refund_state'] = '申请失败';
                break;
            default:
                $arr_data['apply_refund_state'] = '';
        }
    }

    //检测状态：0无 1待检测 2检测成功-合格（通过-不退款）3检测成功-不合格（未通过-不退款）9检测失败（不符合检测条件-退款）10检测失败（用户申请退款-退款）
    public function name_check_state($state,&$arr_data){
        switch ($state)
        {
            case 1:
                $arr_data['check_state'] = '待检测';
                break;
            case 2:
                $arr_data['check_state'] = '检测成功-合格';
                break;
            case 3:
                $arr_data['check_state'] = '检测成功-不合格';
                break;
            case 9:
                $arr_data['check_state'] = '检测失败';
                break;
            case 10:
                $arr_data['check_state'] = '检测失败';
                break;
            default:
                $arr_data['check_state'] = '';
        }
    }

    /**
     * 扩展信息
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getExtendAttribute()
    {
        $n_apply_refund_state = intval($this->apply_refund_state);
        $n_check_state = intval($this->check_state);

        $arr_data['apply_refund_state'] = $n_apply_refund_state;
        $arr_data['check_state'] = $n_check_state;
        OrderCheckpointVehicle::deal_for_state($arr_data,$n_can_apply_refund);


//        $s_created_at = $this->created_at;
//        $arr_data               = $this->arr_state_display_name();
//        $arr_data['created_at'] = ymd_friendly_date($s_created_at);
//        $arr_data['api'] = $this->table_field_api();
//
        return $arr_data;
    }


    //-1状态异常 10待检测 20检测成功-合格 30检测成功-不合格 51退款中 52待退款 53已退款
    public function deal_for_state(&$data,&$n_can_apply_refund){
        $n_apply_refund_state = intval($data['apply_refund_state']);
        $n_check_state = intval($data['check_state']);

        self::name_apply_refund_state($n_apply_refund_state,$arr_data);
        self::name_check_state($n_check_state,$arr_data);


        $s_msg = '状态异常';
        $s_msg_full = '未知错误';
        $n_state = -1;
        $n_apply_refund = 2;//用户是否可申请退款 1是 2否
        $n_can_check = 2;//商户是否可检测操作 1是 2否
        $n_can_apply = 2;//商户是否可处理退款申请 1是 2否
        $n_can_refund = 2;//商户是否可退款 1是 2否
        if ($n_check_state == 0){
            //1待检测
            $s_msg = '';
            $s_msg_full = '订单还处于待支付状态';
            $n_state = 0;
        }
        elseif ($n_check_state == 1){
            //1待检测
            $s_msg = '待检测';
            $s_msg_full = '待检测';
            $n_state = 10;
            $n_apply_refund = 1;
            $n_can_check = 1;
        }
        elseif($n_check_state == 2){
            //2检测成功-合格
            $s_msg = '车辆合格';
            $s_msg_full = '检测成功-合格';
            $n_state = 20;
        }
        elseif($n_check_state == 3){
            //3检测成功-不合格
            $s_msg_full = '检测成功-不合格';
            $s_msg = '车辆不合格';
            $n_state = 30;
        }
        elseif($n_check_state == 9){
            //9检测失败（不符合检测条件-退款）
            if ($n_apply_refund_state == 2){
                //待退款
                $s_msg = '故障（待退款）';
                $s_msg_full = '不符合检测条件-待退款';
                $n_state = 52;
                $n_can_refund = 1;
            }
            elseif($n_apply_refund_state == 3){
                //已退款
                $s_msg = '故障（已退款）';
                $s_msg_full = '不符合检测条件-已退款';
                $n_state = 53;
            }

        }
        elseif($n_check_state == 10){
            //10检测失败（用户申请退款-退款）

            if ($n_apply_refund_state == 1){
                //申请中
                $s_msg = '退款申请中';
                $s_msg_full = '用户申请退款-申请中';
                $n_state = 51;
                $n_can_apply = 1;
            }
            elseif ($n_apply_refund_state == 2){
                //待退款
                $s_msg = '待退款';
                $s_msg_full = '用户申请退款-待退款';
                $n_state = 52;
                $n_can_refund = 1;

            }
            elseif($n_apply_refund_state == 3){
                //已退款
                $s_msg = '已退款';
                $s_msg_full = '用户申请退款-已退款';
                $n_state = 53;
            }
            elseif($n_apply_refund_state == 9){
                //待检测
                $s_msg = '待检测';
                $s_msg_full = '用户申请退款不通过-待检测';
                $n_state = 10;

                //todo 此时不允许申请退款
//                $n_apply_refund = 1;
                $n_can_check = 1;
            }

        }

        $data['order_state'] = $n_state;
        $data['order_state_name'] = $s_msg;
        $data['order_state_full_name'] = $s_msg_full;
        $data['user_waiting_operate'] = [
          'apply_refund'=>$n_apply_refund,//用户是否可申请退款 1是 2否
        ];

        $data['shop_waiting_operate'] = [
          'can_check'=>$n_can_check,//商户是否可检测操作 1是 2否
          'can_apply'=>$n_can_apply,//商户是否可处理退款申请 1是 2否
          'can_refund'=>$n_can_refund,//商户是否可退款 1是 2否
        ];

        if($n_apply_refund == 1){
            $n_can_apply_refund = 1;
        }

    }



    /**
     * state 状态值数组
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function state_arr()
    {
        //申请退款状态：0-无 1-申请中 2-待退款 3-退款成功 9-申请失败
        $arr_apply_refund_state = [
          ['id' => -99, 'name' => '申请退款状态'],
          ['id' => 0, 'name' => '无'],
          ['id' => 1, 'name' => '申请中'],
          ['id' => 2, 'name' => '待退款'],
          ['id' => 3, 'name' => '退款成功'],
          ['id' => 9, 'name' => '申请失败'],
        ];

        //检测状态：0无 1待检测 2检测成功 9检测失败
        $arr_check_state = [
          ['id' => -99, 'name' => '检测状态'],
          ['id' => 0, 'name' => '无'],
          ['id' => 1, 'name' => '待检测'],
          ['id' => 2, 'name' => '检测成功'],
          ['id' => 9, 'name' => '检测失败'],
        ];

        $arr_data = [
          'apply_refund_state' => $arr_apply_refund_state,
          'check_state'        => $arr_check_state,
        ];
        return $arr_data;
    }

    /**
     * 获取state值和注释的集合
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function arr_state_name()
    {
        return $this->hi_arr_state_name($this->state_arr());
    }

    /**
     * 获取state值和注释的集合
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function arr_state_display_name()
    {
        return $this->hi_arr_state_display_name($this->state_arr());
    }



}